package stu.cn.ua.rgr;
import java.io.ObjectInputStream.GetField;

import stu.cn.ua.rgr.Main;
import process.Dispatcher;
import queues.QueueForTransactions;
import queues.TransProcessQueue;
import qusystem.FinishDevice;
import qusystem.MultiActor;
import stat.DiscretHisto;
import widgets.experiments.IExperimentable;
import stat.Histo; 
import stu.cn.ua.rgr.Sivalka;
import transProcess.ITransProcesable;


public class Model implements IExperimentable, ITransProcesable {
	private Main gui;
	private Dispatcher dispatcher;
	private Pogruzchik pogruzchik;
	private TransProcessQueue queueOfSivalka;
	private QueueForTransactions queueOfGruzovikDownload;
	private TransProcessQueue queueOfGruzovikUpload;
	//private TransProcessQueue queueGruzovik;
	//private TransProcessQueue queueSivalka;
	private MultiActor multiSivalka;
	private MultiActor multiGruzovik;
	private DiscretHisto discretHisto;
	private Histo histo55;
	//private double sum;
	//private double lastSumTime;
	//private double accumTime;
	private DiscretHisto histo1 = null;
	private DiscretHisto histo = null;
	
	/*private TransProcessQueue getQueueGruzovik(){
		if (queueGruzovik == null){
			queueGruzovik = new TransProcessQueue();
			queueGruzovik.setNameForProtocol("Черга вантажівок");
			queueGruzovik.setDispatcher(getDispatcher());
		}
		return queueGruzovik;
	}*/
	public DiscretHisto getHisto() {
		if (histo == null) {
			histo = new DiscretHisto();
		}
		return histo;
	}
	public DiscretHisto getHisto1() {
		if (histo1 == null) {
			histo1 = new DiscretHisto();
		}
		return histo1;
	}
	/*private TransProcessQueue getQueueSivalka(){
		if (queueSivalka == null){
			queueSivalka = new TransProcessQueue();
			queueSivalka.setNameForProtocol("Черга сівалок");
			queueSivalka.setDispatcher(getDispatcher());
			//queueSivalka.setDiscretHisto(getH)
		}
		return queueSivalka;
	}*/

	public Model(Dispatcher d, Main g) {
		if (d == null || g == null) {
			System.out.println("Не визначено диспетчера або GUI для Model");
			System.out.println("Подальша робота неможлива");
			System.exit(0);
		}
		dispatcher = d;
		gui = g;
		dispatcher.printToProtocol("Модель створена");
		// Передаємо акторів до стартового списку диспетчера
		componentsToStartList();

	}
	public Dispatcher getDispatcher() {
		if(dispatcher==null){
			System.out.println("Не визначено диспетчера для моделі");
		}
		return dispatcher;
	}
	
	public void setDispatcher(Dispatcher newDispatcher) {
		dispatcher=newDispatcher;
		
	}
	public void componentsToStartList() {
		// Передаємо акторів диспетчеру
		dispatcher.addStartingActor(getPogruzchik());
		dispatcher.addStartingActor(getMultiSivalka());
		dispatcher.addStartingActor(getMultiGruzovik());
	}
	
	public MultiActor getMultiSivalka() {
		if (multiSivalka == null) {
			multiSivalka = new MultiActor();
			// кол-во сеялок
			multiSivalka.setNumberOfClones(Integer.parseInt(gui.getJTextCntSiv().getText()));
			multiSivalka.setOriginal(new Sivalka("Сівалка ", this.gui, this));
		}
		return multiSivalka;
	}
	public MultiActor getMultiGruzovik() {
		if (multiGruzovik == null) {
			multiGruzovik = new MultiActor();
			//задаем кол-во
			multiGruzovik.setNumberOfClones(Integer.parseInt(gui.getJTextCntGr().getText()));
			//multiGruzovik.setNumberOfClones(gui.getChooseData41().getInt());
			multiGruzovik.setOriginal(new Gruzovik("Вантажівка ", this.gui, this));
		}
		return multiGruzovik;
	}
	public Pogruzchik getPogruzchik() {
		if (pogruzchik == null) {
			pogruzchik = new Pogruzchik("Навантажувач ", gui, this);
		}
		return pogruzchik;
	}
	public TransProcessQueue getQueueOfSivalka() {
		
		if (queueOfSivalka == null) {
			queueOfSivalka = new TransProcessQueue();
			queueOfSivalka.setNameForProtocol("Черга сівалок на завантаження ");
			queueOfSivalka.setDispatcher(dispatcher);
			queueOfSivalka.setDiscretHisto(getHisto());
		}
		return queueOfSivalka;
	}
	public QueueForTransactions getQueueOfGruzovikDownload() {
		if (queueOfGruzovikDownload == null) {
			queueOfGruzovikDownload = new QueueForTransactions();
			queueOfGruzovikDownload.setNameForProtocol("Черга вантажівок на загрузку ");
			queueOfGruzovikDownload.setDispatcher(dispatcher);
		}
		return queueOfGruzovikDownload;
	}
	public TransProcessQueue getQueueOfGruzovikUpload() {
		if (queueOfGruzovikUpload == null) {
			queueOfGruzovikUpload = new TransProcessQueue();
			queueOfGruzovikUpload.setNameForProtocol("Черга вантажівок на поле ");
			queueOfGruzovikUpload.setDispatcher(dispatcher);
			queueOfGruzovikUpload.setDiscretHisto(getHisto1());
		}
		return queueOfGruzovikUpload;
	}

	public void initForTest() {
		getQueueOfSivalka().setPainter(gui.getDiagramSiv().getPainter());
		getQueueOfGruzovikUpload().setPainter(gui.getDiagramGrUpload().getPainter());
		getQueueOfGruzovikDownload().setPainter(gui.getDiagramGrDownload().getPainter());
	}
	public void initForStat() {
		getHisto();
		getHisto1();
		
		dispatcher.setProtocolFileName("");
	}
	
	public DiscretHisto getDiscretHisto() {
		if (discretHisto == null) {
			discretHisto = new DiscretHisto();
		}
		return discretHisto;
	}
	
	public Histo getHisto2() {
		if (histo55 == null) {
			histo55 = new Histo();
		}
		return histo55;
	}
	
	public void initForExperiment(double finishTime, double factor) {
		gui.getJTextCntSiv().setText(String.valueOf((int) factor));
		getQueueOfGruzovikUpload().init();
		getQueueOfGruzovikUpload().setPainter(null);
		getQueueOfSivalka().init();
		getQueueOfSivalka().setPainter(null);
		gui.getJTextTime().setText(String.valueOf(finishTime));
		if (gui.getJRadioBChergaAvt().isSelected()){
			getQueueOfGruzovikUpload().setDiscretHisto(getDiscretHisto());
			getDiscretHisto().init();
		} else
		{	
			getHisto2().init();
		}

	}
	
	@Override
	public double getResultOfExperiment() {
	
		if (gui.getJRadioBChergaAvt().isSelected()){
			return getDiscretHisto().getAverage();
		} else
		{	return getHisto2().average();
		}
	}

	public void initForTrans(double d) {
		gui.getJTextTime().setText(String.valueOf(d));
		int modelID = hashCode();
		getQueueOfGruzovikUpload().init();
		getQueueOfGruzovikUpload().setNameForProtocol("Черга вантажівок" + modelID);
		getMultiGruzovik().setNameForProtocol("Вантажівка"+ modelID);
		getMultiSivalka().setNameForProtocol("Сівалка"+ modelID);
		System.out.println("******************************************");
		//getMultiGruzovik().setNumberOfClones(Integer.parseInt(gui.getJTextCntGr().getText()));
		getQueueOfSivalka().init();
		getQueueOfSivalka().setNameForProtocol("Черга сівалок" + modelID);
		//getMultiSivalka().setNumberOfClones(Integer.parseInt(gui.getJTextCntSiv().getText()));
		getQueueOfGruzovikUpload().setPainter(null);
		getQueueOfSivalka().setPainter(null);	
		
	}
	
	public void resetTransAccum() {
		getQueueOfSivalka().resetAccum();
		getQueueOfGruzovikUpload().resetAccum();
	}
	public double getTransResult() {
		if (gui.getJRadioButton2().isSelected()) {
			return getQueueOfGruzovikUpload().getAvg();
		}
		else {
			return getQueueOfSivalka().getAvg();
		}
	}
	/*	public void initForTrans(double finishTime) {
			int modelID = hashCode();
			getQueueSivalka().setNameForProtocol("Черга сівалок на поле" + modelID);
			getMultiSivalka().setNameForProtocol("Сівалка" + modelID);
			getMultiGruzovik().setNameForProtocol("Вантажівка" + modelID);
			gui.getJTextTime().setText(String.valueOf(finishTime));
			
			getQueueSivalka().init();
			getQueueSivalka().setPainter(null);	
			getMultiSivalka().setNumberOfClones(Integer.parseInt(gui.getJTextCntSiv().getText()));
		}*/
	    
	/*public void resetTransAccum() {
		lastSumTime = getDispatcher().getCurrentTime();
		sum = 0;
		accumTime = 0;
		
	}*/
	
	
	/*public double getTransResult() {
		if (gui.getJRadioButton2().isSelected()) {
			double dt = getDispatcher().getCurrentTime() - lastSumTime;
			lastSumTime = getDispatcher().getCurrentTime();
			sum += dt * getQueueGruzovik().size();
			accumTime += dt;
			return sum / accumTime;
		}
		else {
			double dt = getDispatcher().getCurrentTime() - lastSumTime;
			lastSumTime = getDispatcher().getCurrentTime();
			sum += dt * getQueueSivalka().size();
			accumTime += dt;
			return sum / accumTime;
		}
	}
*/

	
}
